package cn.edu.ahu.aircondition.modbus.service.dao;

import cn.edu.ahu.aircondition.modbus.service.entity.AirConStatuWaterEntity;
import cn.edu.ahu.aircondition.modbus.service.entity.AirConStatusEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

/**
 *
 * Created by ahu on 2017/6/11.
 */
public class AirConStatusWaterDao {
    private SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

    /**
     * 根据deviceAddress获取状态信息
     * @param deviceAddressList 设备地址队列
     * @return 设备状态信息
     */
    public List<AirConStatuWaterEntity> listAirConStatusCurByDeviceId(List<Integer> deviceAddressList){
        Transaction transaction = sessionFactory.getCurrentSession().getTransaction();
        transaction.begin();
        Session session = sessionFactory.getCurrentSession();
        //开始事务
        String queryString = "FROM AirConStatuWaterEntity AS airConStatuWaterEntity " +
                "WHERE airConStatuWaterEntity.deviceAddress IN :deviceAddressList";
        List<AirConStatuWaterEntity> list = session.createQuery(queryString,AirConStatuWaterEntity.class).
                setParameter("deviceAddressList",deviceAddressList).getResultList();
        transaction.commit();
        session.close();
        return list;
    }

    /**
     * 新增数据库中空调流水状态信息
     * @param airConStatusWaterEntityList    空调状态信息List
     */
    public void saveAirConStatusCur(List<AirConStatuWaterEntity> airConStatusWaterEntityList){
        Transaction transaction = sessionFactory.getCurrentSession().getTransaction();
        transaction.begin();
        Session session = sessionFactory.getCurrentSession();
        for (AirConStatuWaterEntity airConStatusWaterEntity : airConStatusWaterEntityList){
            session.save(airConStatusWaterEntity);
        }
        transaction.commit();
        session.close();
    }
}
